<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="组织全称" prop="organizationFullName">
        <el-input v-model="queryParams.organizationFullName" placeholder="请输入组织全称" clearable style="width: 200px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="organizationState">
        <el-select v-model="queryParams.organizationState" placeholder="部门状态" clearable style="width: 200px">
          <el-option v-for="(dict,index) in ['正常','注销']" :key="dict" :label="dict" :value="index" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        <el-button type="primary" plain icon="Plus" @click="handleAdd(undefined)" v-hasPermi="['system:dept:add']">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="deptList"
      row-key="organizationId"
      :default-expand-all="isExpandAll"
      height="55vh"
    >
      <el-table-column prop="organizationFullName" width="200" label="组织全称"></el-table-column>
      <el-table-column prop="organizationCode" label="组织编码"></el-table-column>
      <el-table-column prop="organizationAbbreviationName" label="组织简称"></el-table-column>
      <el-table-column prop="organizationType" label="组织类型"></el-table-column>
      <el-table-column prop="organizationState" label="状态">
        <template #default="scope">
          <span>{{['正常','注销'][scope.row.organizationState]}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注"></el-table-column>
      <el-table-column prop="updateUser" label="最后更新人"></el-table-column>
      <el-table-column prop="updateTime" width="200" label="最后更新时间" ></el-table-column>
      <el-table-column label="操作" align="center" width="300" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:dept:edit']">修改</el-button>
          <el-button link type="primary" icon="Plus" @click="handleAdd(scope.row)" v-hasPermi="['system:dept:add']">新增</el-button>
          <el-button link type="primary" icon="Delete" @click="handlezx(scope.row)" v-hasPermi="['system:dept:add']">注销</el-button>
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:dept:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改部门对话框 -->
    <el-dialog :title="title" v-model="open" width="60%" append-to-body>
      <el-form ref="deptRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="8" v-if="!form.isShow">
            <el-form-item label="上级部门" prop="parentId" >
              <el-tree-select
                v-model="form.parentId"
                :data="deptOptions"
                :props="{ value: 'organizationId', label: 'organizationFullName', children: 'children' }"
                value-key="organizationId"
                placeholder="选择上级部门"
                check-strictly
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="组织全称" prop="organizationFullName">
              <el-input v-model="form.organizationFullName" placeholder="请输入部门名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="组织简称" prop="organizationAbbreviationName">
              <el-input v-model="form.organizationAbbreviationName" placeholder="请输入组织类型"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="组织类型" prop="organizationType">
              <el-input v-model="form.organizationType" placeholder="请输入组织类型"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="组织编码" prop="organizationCode">
              <el-input v-model="form.organizationCode" placeholder="请输入组织编码"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Dept">
import { logoutById } from "@/api/main/dataDictionary";
import { listDept, getDept, delDept, addDept, updateDept, listDeptExcludeChild } from "@/api/system/dept";

const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");

const deptList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const title = ref("");
const deptOptions = ref([]);
const isExpandAll = ref(true);
const refreshTable = ref(true);

const data = reactive({
  form: {},
  queryParams: {
    organizationFullName: undefined,
    organizationState: undefined,
  },
  rules: {
    organizationFullName: [{ required: true, message: "组织全称不能为空", trigger: ["blur", "change"] }],
    organizationCode: [{ required: true, message: "组织编码不能为空", trigger: ["blur", "change"] }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询部门列表 */
function getList() {
  loading.value = true;
  listDept(queryParams.value).then((response) => {
    deptList.value = response.data
    deptOptions.value = response.data
    // deptList.value = proxy.handleTree(response.data, "organizationId");
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    organizationCode: undefined,
    parentId: undefined,
    orderNum: 0,
    leader: undefined,
    phone: undefined,
    email: undefined,
    organizationState: "0",
  };
  proxy.resetForm("deptRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 新增按钮操作 */
function handleAdd(row) {
  reset();
  // listDept().then((response) => {
  //   deptOptions.value = response.data
  //   // deptOptions.value = proxy.handleTree(response.data, "organizationId");
  // });
  if (row != undefined) {
    form.value.parentId = row.organizationId;
    form.value.isShow = false
  } else {
    form.value.parentId = 0;
    form.value.isShow = true
  }
  console.log(row,form.value.isShow)
  open.value = true;
  title.value = "添加部门";
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  // listDeptExcludeChild(row.organizationId).then((response) => {
    
    // deptOptions.value = proxy.handleTree(response.data, "organizationId");
    // getDept(row.organizationId).then((response) => {
      form.value = row;
      open.value = true;
      title.value = "修改组织";
      console.log(response.data)
    // });
  // });
  
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["deptRef"].validate((valid) => {
    if (valid) {
      if (form.value.organizationId != undefined) {
        updateDept(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addDept(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
function handlezx(row){
  proxy.$modal
    .confirm('是否确认注销名称为"' + row.organizationType + '"的数据项?')
    .then(function () {
      return logoutById({id:row.organizationId});
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("注销成功");
    })
    .catch(() => {});
}
/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal
    .confirm('是否确认删除名称为"' + row.organizationType + '"的数据项?')
    .then(function () {
      return delDept({id:row.organizationId});
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

getList();
</script>
